package com.yh.tea.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yh.tea.entity.*;
import com.yh.tea.mapper.UserMapper;
import com.yh.tea.model.PageDTO;
import com.yh.tea.model.UserQuery;
import com.yh.tea.model.UserVO;
import com.yh.tea.service.*;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yh.tea.utiity.JWTUtility;
import com.yh.tea.utiity.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yh
 * @since 2024-02-15
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private JWTUtility jwtUtility;

    @Override
    public ResponseResult login(User user) {
        String result;
        //判断user是否正确
        User user1=getOne(new QueryWrapper<User>().eq("user_name",user.getUserName()).eq("password",user.getPassword()));
        System.out.println(user1);
        if(null==user1){
            result="用户名或者密码不正确";
            return ResponseResult.fail(result);
        }else {
            //生成token
            result=jwtUtility.createJWT(user1);
        }
        return ResponseResult.success("登陆成功",result);
    }

    @Override
    public ResponseResult getUserId(String token) {
        User user=jwtUtility.ReadJWT(token);
        return ResponseResult.success("查询成功",user.getId());
    }

    @Override
    public ResponseResult getUserList(UserQuery query) {
        // 1. 准备分页查询条件
        // 1.1.分页条件
        Page<User> page = Page.of(query.getPageNo(), query.getPageSize());
        // 1.2.排序条件
        if(query.getSortBy().isEmpty()){
            // 如果排序字段为空，按照更新时间排序
            page.addOrder(new OrderItem("update_time", false));
        } else {
            page.addOrder(new OrderItem(query.getSortBy(), query.getIsAsc()));
        }
        //模糊查询寻
        if(query.getUserName().isEmpty()){
            this.page(page);

        }else {
            this.page(page,new QueryWrapper<User>().like("user_name",query.getUserName()));
        }
        // 2. 查询，查询的接口将封装到 page 对象中。
        // 3. 数据非空校验
        List<User> users = page.getRecords();

        if(users == null || users.isEmpty()){
            return ResponseResult.success("",new PageDTO<UserVO>(page.getPages(),page.getTotal(),new ArrayList()));
        }
        // 4. 存在数据，进行实体类转换
        List<UserVO> userVOList=new ArrayList();
        for (User user : users) {
            userVOList.add(new UserVO(user.getId(),user.getUserName(),user.getRegistrationDate()));
        }
        // 5. 封装返回结果
        return ResponseResult.success("",new PageDTO<UserVO>(page.getTotal(),page.getPages(),userVOList));

    }

    @Override
    public ResponseResult getUserByIds(List<Long> userIdList) {
        return ResponseResult.success("查询成功",listByIds(userIdList));
    }


}
